package cn.edu.zjicm.wordsnet_d.synchronize;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import cn.edu.zjicm.wordsnet_d.activity.FeedBackActivity;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.util.LogUtil;
import android.content.Context;
import android.os.Handler;

public class FeedBackInterface extends ServerInterface {

	private static FeedBackInterface mInstance;

	public static FeedBackInterface getInstance(Context context) {
		if (mInstance == null) {
			mInstance = new FeedBackInterface(context);
		}
		return mInstance;
	}

	private FeedBackInterface(Context context) {
		super(context);
	}

	public void submitFeekback(String feedback, String contact, Handler handler) {
		String token = AppPreference.getToken(context);
		Map<String, Object> map = new HashMap<String, Object>();
		if (!token.equals("") && !token.equals("guest")) {
			map.put("t", token);
		}
		map.put("feedback", feedback);
		if (contact != null && contact.length() != 0) {
			map.put("contact", contact);
		}
		new FeedbackThread(map, handler).start();
	}

	public class FeedbackThread extends Thread {
		Map<String, Object> map;
		Handler handler;
		int what;

		public FeedbackThread(Map<String, Object> map, Handler handler) {
			this.map = map;
			this.handler = handler;
		}

		@Override
		public void run() {
			DefaultHttpClient client = new DefaultHttpClient();
			UserInterface.setClientTimeout(client);

			List<NameValuePair> pairList = new ArrayList<NameValuePair>();
			NameValuePair nameValuePair;
			Iterator it = map.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry entry = (Entry) it.next();
				nameValuePair = new BasicNameValuePair((String) entry.getKey(), (String) entry.getValue());
				pairList.add(nameValuePair);
			}

			try {
				HttpEntity entity = new UrlEncodedFormEntity(pairList, "utf-8");
				HttpPost post = new HttpPost(FEED_BACK_INNER);
				post.setEntity(entity);
				post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf8");
				HttpResponse response = client.execute(post);
				int statusCode = response.getStatusLine().getStatusCode();

				if (statusCode == 200) {
					String str = EntityUtils.toString(response.getEntity(), "utf-8");
					JSONObject result = new JSONObject(str);
					boolean success = result.getBoolean("success");
					if (success) {
						what = FeedBackActivity.MSG_SUCCESS;
					} else {
						what = FeedBackActivity.MSG_UNSUCCESS;
					}
				} else {
					what = FeedBackActivity.MSG_UNSUCCESS;
				}
			} catch (Exception e) {
				e.printStackTrace();
				what = FeedBackActivity.MSG_BAD_NEWWORK;
			}
			handler.sendMessage(handler.obtainMessage(what));
		}
	}
}
